package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteNoteEsService;
import cn.wolfcode.wolf2w.business.api.RemoteNoteService;
import cn.wolfcode.wolf2w.business.api.RemoteStrategyEsService;
import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.service.INoteContentService;
import cn.wolfcode.wolf2w.business.service.impl.NoteContentServiceImpl;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 旅游日记 Controller
 * 
 * @author yuhao
 * @date 2025-09-16
 */
@RestController
@RequestMapping("notes")
public class NoteController {
    @Autowired
    private INoteService noteService;
    @Autowired
    private RemoteNoteEsService remoteNoteEsService ;
    @Autowired
    private RemoteUserInfoService remoteUserInfoService;
    @Autowired
    private INoteContentService noteContentService;

    /*顶*/
    @PostMapping("/star/{id}")
    @RequiresLogin
    public R<Map<String, Object>> star(@PathVariable Long id){
        Map<String, Object> map=noteService.star(id);
        return R.ok(map);
    }

    //添加游记
    @PostMapping("/add")
    @RequiresLogin
    public R<Map<String, Object>> add(@RequestBody Note  note){
        noteService.add(note);
        return R.ok();
    }

    /*收藏*/
    @PostMapping("/collect/{id}")
    @RequiresLogin
    public R<Map<String, Object>> collect(@PathVariable Long id){
        Map<String, Object> map =noteService.collect(id);
        return R.ok(map);
    }

    /*取消收藏*/
    @PostMapping("/uncollect/{id}")
    @RequiresLogin
    public R<Map<String, Object>> uncollect(@PathVariable Long id){
        Map<String, Object> map=noteService.uncollect(id);
        return R.ok(map);
    }


    /**
     * 游记数据初始化到ES库中
     */
    //正常需要加在管理员的controller
    @GetMapping("/dataInit")
    public R<?> dataInit(){
        //查询mysql中 的数据写入到es中，需要一个和es对应的服务
        //不写在这个服务里面就是避免一个服务调用两个 服务的事情
        List<Note> list = noteService.list();
        for (Note note : list) {
           NoteEs es = new NoteEs();
            //同名属性拷贝
            BeanUtils.copyProperties(note, es);
            //初始化
            remoteNoteEsService.initNoteEs(es, SecurityConstants.INNER);
        }
        return R.ok("攻略数据初始化完成");
    }

    @GetMapping("/viewnnumTop3")
    public R<List<Note>> viewnnumTop3(Long destId) {
        List<Note> list= noteService.queryViewnumTop3(destId);
        return R.ok(list);
    }


    /**
     * 旅游日记详情
     */
    @GetMapping("/detail/{id}")
    @RequiresLogin
    public R<Note> detail(@PathVariable Long id) {
        Note note = noteService.getById(id);
        Long userId = SecurityContextHolder.getUserId();
        R<UserInfo> one = remoteUserInfoService.getOne(userId, SecurityConstants.INNER);
        UserInfo data = one.getData();
        note.setAuthor(data);
        NoteContent content = noteContentService.getById(note.getId());
        note.setContent(content);
        return R.ok(note);
    }
    /**
     * 旅游日记 列表
     */
    @GetMapping("/query")
    public R<IPage<Note>> query(NoteQuery qo) {
        IPage<Note> page = noteService.queryPage(qo);
        return R.ok(page);
    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Note>> feignList() {
        return R.ok(noteService.list());
    }

    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Note> feignGet(@PathVariable Long id) {
        return R.ok(noteService.getById(id));
    }

    @GetMapping("/note/feign")
    public R<?> noteStar(){
        noteService.noteStar();
        return R.ok();
    }

}
